<template>
  <el-dialog
    title="选择品牌"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-table :data="list" border>
      <el-table-column label="编号" width="120" align="center" prop="id">
      </el-table-column>
      <el-table-column label="品牌名称" align="center" prop="name">
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleAddBrand(scope.row)"
            >添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next,sizes, jumper"
    ></el-pagination>
    <div slot="footer">
      <el-button size="small" @click="visible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="visible = false"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import BrandApi from "@/api/product/brand";
import BrandRecommentApi from "@/api/marketing/brandrecomment";

export default {
  data() {
    return {
      visible: false,
      list: [],
      pageIndex: 1,
      pageSize: 5,
      total: 0,
    };
  },
  methods: {
    init() {
      this.visible = true;
      this.getDataList();
    },
    handleAddBrand(row) {
      BrandRecommentApi.createBrandRecomment({ brandId: row.id }).then(
        ({ data }) => {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            },
          });
        }
      );
    },
    getDataList() {
      this.listLoading = true;
      BrandApi.fetchList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      }).then((response) => {
        this.list = response.data.list;
        this.total = response.data.totalCount;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
  },
};
</script>
